const header = new Vue({
    el: "#nav",
    data: {
        isLogin: false,
        username: "",
        userPicture: "",
        publishUrl: "",
        unread: 0,
        isShow: 'none'
    },

    created() {
        this.checkIsLogin();
    },
    methods: {
        //检验是否已经登录==>显示头像框和消息icon
        checkIsLogin: function() {
            axios.get("/user/isLogin").then(response => {
                //获取json数据
                let resultInfo = response.data;

                //如果已经登录的话，改变已登录状态
                if (resultInfo.status === 1) {
                    //用户的头像地址
                    this.userPicture = resultInfo.data.userPicture;

                    //用户姓名
                    this.username = resultInfo.data.username;

                    this.isLogin = true;

                    this.getUnread(true);

                    this.publishUrl = "/share/browse.html?type=publish&key=";
                }

            }).catch();
        },

        //获取用户的未读信息
        getUnread: function(isLogin) {
            //判断是否登录之后，才进行获取未读消息
            if (isLogin === true) {
                axios.get("/chat/unread").then(response => {
                    //获取json数据
                    let resultInfo = response.data;
                    //未读数
                    this.unread = resultInfo.data;
                }).catch();
            } else {
                console.log("请先登录");
            }

        },

        // 退出登录
        logout: function() {
            axios.get("/user/logout").then(response => {
                let resultInfo = response.data;
                if (resultInfo.status === 1) {
                    location.href = "/index.html";
                }
            })
        },

        // 提示用户进行登录
        loginTip: function() {
            if (this.isLogin === false) {
                layer.msg("请先登录", { time: 5000 });
            } else {
                location.href = this.publishUrl;
            }
        },

        // 展示消息与个人中心
        showMenu: function() {
            this.isShow = 'block';
        },
        hiddenMenu: function() {
            this.isShow = 'none';

        }


    }
})